﻿Imports System.Data.OleDb


Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HatConnStr").ConnectionString)
            Dim SqlString As String = "SELECT * FROM TBL_Profile WHERE UserName=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then TB_FirstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then TB_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("Gender")) Then RB_Gender.SelectedValue = dr("Gender")
                If Not IsDBNull(dr("DOB")) Then TB_DOB.Text = dr("DOB")
                If Not IsDBNull(dr("Description")) Then TB_Description.Text = dr("Description")
            End If
        End If
    End Sub

    Protected Sub bt_Submit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_Submit.Click

        Dim SqlString As String
        Dim newFileName As String

        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_RPicture.FileName)
        If fup_RPicture.HasFile Then
            fup_RPicture.SaveAs(Server.MapPath("Image") & "/" & newFileName)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("HatConnStr").ConnectionString)
        If fup_RPicture.HasFile Then

            SqlString = "UPDATE TBL_Profile SET FirstName=@f1, LastName=@f2, UserPicture=@f3, Gender=@f4, DOB=@f5, Description=@f6 WHERE UserName=@f7"
        Else
            SqlString = "UPDATE TBL_Profile SET FirstName=@f1, LastName=@f2, Gender=@f4, DOB=@f5, Description=@f6 WHERE UserName=@f7"
        End If


        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        Try
            cmd.Parameters.AddWithValue("@f1", TB_FirstName.Text)
            cmd.Parameters.AddWithValue("@f2", TB_LastName.Text)
            If fup_RPicture.HasFile Then cmd.Parameters.AddWithValue("@f3", newFileName)
            cmd.Parameters.AddWithValue("@f4", RB_Gender.SelectedValue)
            cmd.Parameters.AddWithValue("@f5", TB_DOB.Text)
            cmd.Parameters.AddWithValue("@f6", TB_Description.Text)
            cmd.Parameters.AddWithValue("@f7", User.Identity.Name)
            oleDbConn.Open()
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            oleDbConn.Close()
            oleDbConn.Dispose()
            Response.Redirect("Profile.aspx?UserName=" & User.Identity.Name)
        Catch ex As Exception
            lbl_Message.Visible = True
            lbl_Message.Text = "Profile was not updated please check your data"
        End Try

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

End Class